/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aqu.persistencia.utilidad;

import aqu.persistencia.entity.*;
import aqu.persistencia.utilidad.entity.Ciudad;
import aqu.persistencia.utilidad.entity.Provincia;
import aqu.persistencia.utilidad.entity.TipoGasto;
import aqu.persistencia.utilidad.entity.TipoIdentificacion;
import java.util.Properties;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Environment;

/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author Verito Ramirez
 *         Victor Jaramillo
 */
public class HibernateUtil {

    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {

        if (sessionFactory == null) {
            try {
                // Create the SessionFactory from standard (hibernate.cfg.xml)
                // config file.
                AnnotationConfiguration ac = new AnnotationConfiguration();
                ac.addAnnotatedClass(Livro.class);                
                ac.addAnnotatedClass(SegUsuario.class);
                ac.addAnnotatedClass(ConEmpresa.class);
                ac.addAnnotatedClass(SegPerfil.class);
                ac.addAnnotatedClass(SegMenu.class);
                ac.addAnnotatedClass(ConEstadoCabecera.class);
                ac.addAnnotatedClass(ConLugarAplicacion.class);
                ac.addAnnotatedClass(ConNivelCuenta.class);
                ac.addAnnotatedClass(ConTipoAsiento.class);
                ac.addAnnotatedClass(ConTipoCuenta.class);
                ac.addAnnotatedClass(ConCuenta.class);
                ac.addAnnotatedClass(SegGlobal.class);
                ac.addAnnotatedClass(ConCabeceracontable.class);
                ac.addAnnotatedClass(ConDetallecontable.class);
                ac.addAnnotatedClass(CxpEstadoFactura.class); 
                ac.addAnnotatedClass(CxpTipoContribuyente.class);
                ac.addAnnotatedClass(CxpProveedor.class);
                ac.addAnnotatedClass(CxpRetencion.class);
                ac.addAnnotatedClass(CxpTipoRetencion.class); 
                ac.addAnnotatedClass(CxpEstadoNota.class); 
                ac.addAnnotatedClass(CxpImpuesto.class); 
                ac.addAnnotatedClass(CxpTarifaAdicional.class); 
                ac.addAnnotatedClass(CxpTipoDocumento.class); 
                ac.addAnnotatedClass(CxpTipoNota.class); 
                ac.addAnnotatedClass(CxpTipoTransaccion.class); 
                ac.addAnnotatedClass(CxpEstadoRetencion.class);
                ac.addAnnotatedClass(CxpDatoFactura.class);
                ac.addAnnotatedClass(CxpDatoAdicionalFactura.class);
                ac.addAnnotatedClass(CxpTipoContribuyenteRetencion.class);
                ac.addAnnotatedClass(CxpCabeceraFactura.class);
                ac.addAnnotatedClass(CxpDetalleFactura.class);
                ac.addAnnotatedClass(CxpCabeceraRetencion.class);
                ac.addAnnotatedClass(CxpDetalleRetencion.class);
                ac.addAnnotatedClass(CxcCliente.class);
                ac.addAnnotatedClass(CxcCabeceraRetencion.class);
                ac.addAnnotatedClass(CxcDetalleRetencion.class);
                ac.addAnnotatedClass(CxcEstadoFactura.class);
                ac.addAnnotatedClass(CxcCabeceraFactura.class);
                ac.addAnnotatedClass(CxcDetalleFactura.class);
                ac.addAnnotatedClass(Provincia.class);
                ac.addAnnotatedClass(Ciudad.class);
                ac.addAnnotatedClass(TipoGasto.class);
                ac.addAnnotatedClass(TipoIdentificacion.class);
                ac.addProperties(getProperties());
                //sessionFactory = ac.configure().buildSessionFactory();
                sessionFactory = ac.buildSessionFactory();
                //SchemaExport se = new SchemaExport(ac);
                //se.create(true, true);


            } catch (Throwable ex) {
                // Log the exception.
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }

            return sessionFactory;

        } else {
            return sessionFactory;
        }

    }

    private static Properties getProperties() {
        Properties properties = new Properties();
        properties.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
        //properties.setProperty(Environment.DRIVER, "org.apache.derby.jdbc.ClientDriver");
        //properties.setProperty(Environment.URL, "jdbc:derby://localhost:1527/sample");
        properties.setProperty(Environment.URL, "jdbc:mysql://localhost:3306/qsotf");
        properties.setProperty(Environment.USER, "root");
        properties.setProperty(Environment.PASS, "admin");
        properties.setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQLDialect");
        //properties.setProperty(Environment.DIALECT, "org.hibernate.dialect.DerbyDialect");
        //properties.setProperty(Environment.C3P0_IDLE_TEST_PERIOD, "org.hibernate.dialect.MySQLDialect");
        //properties.setProperty(Environment.C3P0_ACQUIRE_INCREMENT, "org.hibernate.dialect.MySQLDialect");
        properties.setProperty(Environment.C3P0_MAX_SIZE, "100");
        properties.setProperty(Environment.C3P0_MIN_SIZE, "5");
        // properties.setProperty(Environment.C3P0_MAX_STATEMENTS, "");
        properties.setProperty(Environment.C3P0_TIMEOUT, "60");


        return properties;
    }
}
